'use strict';

angular.module('CBO').controller('WorkLocationListController', ['$scope', 'BaseModule', 'BaseEnum', '$location', 'Authentication',
    function($scope, BaseModule, BaseEnum, $location, Authentication) {
    	BaseModule.initModule($scope, 'WorkLocation');
        $scope.Module.WorkLocation.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.WorkLocation.PageSize = 15;
        $scope.Module.WorkLocation.Populate = {path: 'Department', select: 'Name'};

        $scope.btnAdd = function(){
        	$location.url('/index/WorkLocation/Edit');
        };

        $scope.rowDblClick = function(){
            $scope.$apply(function(){
                $location.url("/index/WorkLocation/Edit/" + $scope.Module.WorkLocation.FocusedRecord._id);
            });
        };

        $scope.btnDelete = function(){
        	$scope.Module.WorkLocation.deleteSelecteds();
        };

        BaseModule.initModule($scope, '_Work');
        BaseModule.initModule($scope, 'StationWork');
        $scope.Module.StationWork.OrderBy = {Work: 1};
        $scope.Module.StationWork.Populate = {
            path: 'Work',
            select: 'Code Name'
        };

        $scope.btnFindWorks = function(){
            if($scope.Module.WorkLocation.FocusedRecord != null){
                $scope.Module.StationWork.find(
                    {WorkLocation: $scope.Module.WorkLocation.FocusedRecord._id}, 
                    $scope.Module.StationWork.OrderBy,
                    $scope.Module.StationWork.Populate,
                    function(records){
                        $scope.Module._Work.Records = [];
                        for(var i=0; i<records.length; i++){
                            if(records[i].Work != null){
                                var newRecord = $scope.Module._Work.add();
                                newRecord.Code = records[i].Work.Code;
                                newRecord.Name = records[i].Work.Name;
                            }
                        }
                        $scope.Module._Work.emitOnLoadEvent();
                    }
                );
            }
        };
    }
]);